import os from 'os';

export default class StringBuffer {
    constructor(s) {
        this.reset(s);
    }

    add(s) {
        if (s) {
            this.data.push(s);
        }
        return this;
    }

    line(s) {
        if (s) {
            this.data.push(s);
        }
        this.data.push(os.EOL);
        return this;
    }

    lines() {
        return this.toString().split(os.EOL);
    }

    empty() {
        return this.data.length === 0;
    }

    clear() {
        this.data.length = 0;
        return this;
    }

    reset(s) {
        this.data = [];
        if (s) {
            this.data.push(s);
        }
    }

    toString(trim) {
        const s = this.data.join('');
        return trim ? s.trim() : s;
    }

    str(trim) {
        return this.toString(trim);
    }
}
